package com.atguigui.leetcode;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 515.在每个树行中找最大值
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by WJX on 2022/6/24 9:50
 */
public class P515FindLargestValueInEachTreeRow {
    public static void main(String[] args) {
        Solution solution = new P515FindLargestValueInEachTreeRow().new Solution();
        // TO TEST
    }

    class Solution {

        Map<Integer, Integer> result = new TreeMap<>();

        public List<Integer> largestValues(TreeNode root) {

            int deep = 0;
            getDiGui(root, deep);

            List<Integer> list = new ArrayList<>();
            for (Integer integer : result.keySet()) {
                list.add(result.get(integer));
            }


            return list;
        }

        private void getDiGui(TreeNode root, int deep) {
            if (root == null) {
                return;
            }
            deep++;
            if (result.containsKey(deep)) {

                Integer integer = result.get(deep);

                result.put(deep, Math.max(integer, root.val));

            } else {
                result.put(deep, root.val);
            }
            getDiGui(root.left, deep);
            getDiGui(root.right, deep);
        }
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
